#include "OpenGLRender.h"
#include <windows.h>
#include <GL/GL.h>

namespace
{
	void glDrawCube(const tfapi::FrameParameters& params)
	{
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();

		auto pm = params.View->ProjectionMatrix.GetTranspose();
		glLoadMatrixf(&pm.m11);
		
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();

		auto vm = params.View->ViewMatrix.GetTranspose();
		glLoadMatrixf(&vm.m11);

		float h = 0.1f; 

		glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
		// Top face (y = 1.0f)
		glColor3f(0.0f, 1.0, 0.0f);     // Green
		glVertex3f(h, h, -h);
		glVertex3f(-h, h, -h);
		glVertex3f(-h, h, h);
		glVertex3f(h, h, h);
		// Bottom face (y = -h)
		glColor3f(1.0, 0.5f, 0.0f);     // Orange
		glVertex3f(h, -h, h);
		glVertex3f(-h, -h, h);
		glVertex3f(-h, -h, -h);
		glVertex3f(h, -h, -h);
		// Front face  (z = h)
		glColor3f(1.0, 0.0f, 0.0f);     // Red
		glVertex3f(h, h, h);
		glVertex3f(-h, h, h);
		glVertex3f(-h, -h, h);
		glVertex3f(h, -h, h);
		// Back face (z = -h)
		glColor3f(1.0, 1.0, 0.0f);     // Yellow
		glVertex3f(h, -h, -h);
		glVertex3f(-h, -h, -h);
		glVertex3f(-h, h, -h);
		glVertex3f(h, h, -h);
		// Left face (x = -h)
		glColor3f(0.0f, 0.0f, 1.0);     // Blue
		glVertex3f(-h, h, h);
		glVertex3f(-h, h, -h);
		glVertex3f(-h, -h, -h);
		glVertex3f(-h, -h, h);
		// Right face (x = h)
		glColor3f(1.0, 0.0f, 1.0);     // Magenta
		glVertex3f(h, h, -h);
		glVertex3f(h, h, h);
		glVertex3f(h, -h, h);
		glVertex3f(h, -h, -h);
		glEnd();  // End of drawing color-cube
		
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		glPopMatrix();
	}
}

void OpenGLRender::OnDirectDraw(tfapi::RenderState state, const tfapi::FrameParameters& params)
{
	if (state == tfapi::rs_Solid)
	{
		glDrawCube(params);
	}
}